***********************************************************************************
* This code obtain datasets containing emission 
* by urban and rural, for 90, 00, 2011, 2014, 2017.
*
* Note: 
*	1. Final FIPS is the merge across - NEI and MSA.
*	2. MSA 2010 used to link with NEI 2011.
*	3. MSA 2015 used to link with NEI 2014.
*	4. MSA 2018 used to link with NEI 2017.
***********************************************************************************
clear all
version 11
set more off

* set data fir
cd ~\data_in


**************************
**************************
* set year here
* 90 and 00
**************************
**************************
local year 1990

use emission_county_level_`year', clear // NEI FIPS is the master list
merge 1:1 fips using data_msa_fips_`year'
drop if _merge==2 // restrict to NEI FIPS, by dropping MSA FIPS not in NEI
replace flag_msa=0 if flag_msa==.
tab flag_msa
drop _merge 
order nox voc so2 nh3 pm25, last
collapse (sum) nox-pm25, by(flag_msa)

outsheet using ..\data_emission_urban_rural_`year'.csv, replace names comma


**************************
**************************
* 2011 NEI linked with
* 2010 MSA
**************************
**************************
use emission_county_level_2011, clear // NEI FIPS is the master list
merge 1:1 fips using data_msa_fips_2010
drop if _merge==2 // restrict to NEI FIPS, by dropping MSA FIPS not in NEI
replace flag_msa=0 if flag_msa==.
tab flag_msa
drop _merge 
order nox voc so2 nh3 pm25, last
collapse (sum) nox-pm25, by(flag_msa)

outsheet using ..\data_emission_urban_rural_2011.csv, replace names comma


**************************
**************************
* 2014 NEI linked with
* 2015 MSA
**************************
**************************
use emission_county_level_2014, clear // NEI FIPS is the master list
merge 1:1 fips using data_msa_fips_2015
drop if _merge==2 // restrict to NEI FIPS, by dropping MSA FIPS not in NEI
replace flag_msa=0 if flag_msa==.
tab flag_msa
drop _merge 
order nox voc so2 nh3 pm25, last
collapse (sum) nox-pm25, by(flag_msa)

outsheet using ..\data_emission_urban_rural_2014.csv, replace names comma



**************************
**************************
* 2017 NEI linked with
* 2018 MSA
**************************
**************************
use emission_county_level_2017, clear // NEI FIPS is the master list
merge 1:1 fips using data_msa_fips_2018
drop if _merge==2 // restrict to NEI FIPS, by dropping MSA FIPS not in NEI
replace flag_msa=0 if flag_msa==.
tab flag_msa
drop _merge 
order nox voc so2 nh3 pm25, last
collapse (sum) nox-pm25, by(flag_msa)

outsheet using ..\data_emission_urban_rural_2017.csv, replace names comma




